{% extends 'myadmin/index.html' %}

{% block title %}
<title>BookShop-图书商品管理</title>
{% endblock %}

{% block con %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">图书商品列表</div>
                </div>

                <div class="widget-body  am-fr">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <a href="{% url 'myadmin_bookadd' %}" class="am-btn am-btn-default am-btn-success">
                                        <span class="am-icon-plus"></span> 新增
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                <form action="">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select name="selecttype" data-am-selected="{btnSize: 'sm'}" style="display: none;">
                              <option value="all" {%if request.GET.selecttype == 'all'%} selected {%endif%}>书名/作者</option>
                              <option value="title" {%if request.GET.selecttype == 'title'%} selected {%endif%}>书名</option>
                              <option value="author" {%if request.GET.selecttype == 'author'%} selected {%endif%}>作者</option>
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input type="text" name="keywords" value="{{ request.GET.keywords }}" class="am-form-field ">
                            <span class="am-input-group-btn">
                                <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
                            </span>
                        </div>
                    </div>
                </form>

                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>封面图</th>
                                    <th>书名</th>
                                    <th>作者</th>
                                    <th>定价</th>
                                    <th>库存</th>
                                    <th>ISBN</th>
                                    <th>出版社</th>
                                    <th>出版时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>

                            <tbody>
                            {% for i in data %}
                                <tr class="gradeX">
                                    <td class="am-text-middle bookid">{{ i.id }}</td>
                                    <td>
                                        <img src="{{ i.bookimgs_set.first.img_url }}" class="tpl-table-line-img" alt="">
                                    </td>
                                    <td class="am-text-middle">{{ i.title }}</td>
                                    <td class="am-text-middle">{{ i.author }}</td>
                                    <td class="am-text-middle">{{ i.price }}</td>
                                    <td class="am-text-middle">{{ i.num }}</td>
                                    <td class="am-text-middle">{{ i.ISBN }}</td>
                                    <td class="am-text-middle">{{ i.publisher }}</td>
                                    <td class="am-text-middle">{{ i.pub_date|date:'Y-m' }}</td>
                                    <td class="am-text-middle">
                                        <div class="tpl-table-black-operation">
                                            <a href="{% url 'myadmin_bookedit' i.id %}">
                                                <i class="am-icon-pencil"></i> 编辑
                                            </a>
                                            <a href="javascript:;" class="tpl-table-black-operation-del bookdelete">
                                                <i class="am-icon-trash"></i> 删除
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>

                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                                {% load pagetag %}
                                {% showpage data.paginator.num_pages request %}
                                    <!-- <li class="am-disabled"><a href="#">«</a></li>-->
                                    <!-- <li class="am-active"><a href="#">1</a></li> -->
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // ajax删除商品
    // 1.为删除绑定单击事件
    $('.bookdelete').click(function(){
        // 2.获取当前点击项中的id
        var bookid = $(this).parents('tr').find('.bookid').text()
        var $this = $(this)
        // 3.发送ajax请求到后台进行删除
        $.get('{% url "myadmin_bookdelete" %}',{'id':bookid},function(data){
            // 4.判断响应的结果
            alert(data['msg'])
            if(data['error'] == 0){
                // 如果后台数据删除成功，则页面中删除该行数据
                // 此时的 %(this) 代表的不再是之前的删除按钮，而是ajax对象，XMLHttpRequest
                $this.parents('tr').remove()
            }
        },'json')
    })
</script>

{% endblock %}